package com.henu.xyh;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.henu.R;
import com.henu.adapter.ViewpageAdapter;
import java.util.ArrayList;

public class FirstusingActivity extends AppCompatActivity {

    private ViewPager viewpage;
    private ArrayList<ImageView> pointlist = new ArrayList<>();
    private ArrayList<View> viewlist = new ArrayList<>();
    private ViewpageAdapter adapter;
    private ImageButton vp_btn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firstusing);

        //获取状态
        get_state();
        //圆点的配置
        ImageView img1 = (ImageView) findViewById(R.id.vp_img1);
        ImageView img2 = (ImageView) findViewById(R.id.vp_img2);
        ImageView img3 = (ImageView) findViewById(R.id.vp_img3);
        //添加至数组pointlist中
        pointlist.add(img1);
        pointlist.add(img2);
        pointlist.add(img3);
        viewpage = (ViewPager) findViewById(R.id.vp);
        adapter = new ViewpageAdapter();
        viewpage.setAdapter(adapter);

        //滑动页面的配置
        View v1 = getLayoutInflater().inflate(R.layout.inflate_vp_item1, null);
        View v2 = getLayoutInflater().inflate(R.layout.inflate_vp_item2, null);
        View v3 = getLayoutInflater().inflate(R.layout.inflate_vp_item3, null);
        //添加指数组viewlist中
        viewlist.add(v1);
        viewlist.add(v2);
        viewlist.add(v3);
        adapter.addData(viewlist);
        adapter.notifyDataSetChanged();

        //实现页面滑动，下面圆点的变换
        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {

                for (int i = 0; i < pointlist.size(); i++) {
                    if (position == i) {
                        ImageView image = pointlist.get(i);
                        image.setBackgroundResource(R.mipmap.dot_b);
                    } else {
                        ImageView image = pointlist.get(i);
                        image.setBackgroundResource(R.mipmap.dot_a);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vp_btn = (ImageButton) v3.findViewById(R.id.vp_btn);
        vp_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存状态
                save_state();
                Intent intent = new Intent(FirstusingActivity.this, SplashActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }



    //保存状态
    public void save_state() {
        SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("is_come", true);
        editor.commit();

    }
    //获取状态
    public void get_state() {
        SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);
        boolean b = sp.getBoolean("is_come", false);
        if (b) {
            Intent intent = new Intent(FirstusingActivity.this, SplashActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
